package weaver.interfaces.zhlx.duss.action.salesmodule.crm;

import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.CrmUtil;
import weaver.interfaces.zhlx.duss.utils.PropUltraUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @author dusongsong
 * @date 2025年5月18日 16:02
 */
public class WeaLhsjlcAction extends BaseBean implements Action {
    private static final String RES_CODE_ZERO = "0";
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaLhsjlcAction>>>>>requestid=" + requestid);
        try {
            RecordSet rs = new RecordSet();
            String message = "调用CRM系统接口返回:";
            CrmUtil crmUtil = new CrmUtil();
            rs.executeQuery("select bh,ssgswb,fqrgh,fqrq,bgsjkh, bgsjwl,bghlh,bgyyxz,bgyy from " + tablename + " where requestid=?",requestid);
            if(rs.next()) {
                String bh = Util.null2String(rs.getString("bh"));//EC流程单号
                String ssgswb = Util.null2String(rs.getString("ssgswb"));//所属公司（文本）
                String fqrgh = Util.null2String(rs.getString("fqrgh"));//发起人工号
                String fqrq = Util.null2String(rs.getString("fqrq"));//发起日期
                String bgsjkh = Util.null2String(rs.getString("bgsjkh"));//客户编号
                String bgsjwl = Util.null2String(rs.getString("bgsjwl"));//变更前料号
                String bghlh = Util.null2String(rs.getString("bghlh"));//变更后料号
                String bgyyxz = Util.null2String(rs.getString("bgyyxz"));//变更后料号
                String bgyy = Util.null2String(rs.getString("bgyy"));//变更原因说明
                JSONObject dataJson = new JSONObject();
                JSONObject object_data = new JSONObject();
                object_data.put("dataObjectApiName", "material_upgrade__c");
                object_data.put("ec_process_number__c", bh);
                object_data.put("company_name__c", ssgswb);
                object_data.put("initiator_code__c", fqrgh);
                fqrq = !"".equals(fqrq) ? fqrq + " 00:00:00" : fqrq;
                object_data.put("date__c", crmUtil.dateToStamp(fqrq));
                object_data.put("customer_code__c", bgsjkh);
                object_data.put("before_part_number__c", bgsjwl);
                object_data.put("changed_part_number__c", bghlh);
                object_data.put("additional_option_valuecus__c", bgyyxz);
                object_data.put("change_reason_description__c", bgyy);
                JSONObject propVal = PropUltraUtil.getPropMainVal("zhlxcrm");
                String username = propVal.getString("username");
                object_data.put("is_deleted", false);
                object_data.put("owner", username.split(","));
                object_data.put("record_type", "default__c");
                dataJson.put("skipDataStatusValidate", true);
                dataJson.put("object_data", object_data);
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("jkdymc", "WeaLhsjlcAction");
                dataMap.put("lcid", requestid);
                dataMap.put("dylc", requestid);
                dataMap.put("cz", src);
                writeLog(">>>>>WeaLhsjlcAction>>>>>dataJson=" + dataJson);
                JSONObject resultJson = crmUtil.excuteHandler(dataJson, dataMap, "WeaLhsjlcAction");
                writeLog(">>>>>WeaLhsjlcAction>>>>>resultJson=" + resultJson);
                String resultMessage = Util.null2String(resultJson.getString("message"));
                if (!"success".equals(resultMessage)) {
                    message += Util.null2String(resultJson.getString("message"));
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                } else {
                    JSONObject resultJsonObj = resultJson.getJSONObject("data");
                    String errorCode = resultJsonObj.getString("errorCode");
                    if (!RES_CODE_ZERO.equals(errorCode)) {
                        message += Util.null2String(resultJsonObj.getString("errorMessage"));
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent(message);
                        return Action.FAILURE_AND_CONTINUE;
                    }
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaLhsjlcAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }
}
